package com.aiyan.edu.service.auth.center.controller;

import com.aiyan.edu.api.auth.AuthControllerApi;
import com.aiyan.edu.framework.domain.ucenter.ext.AuthToken;
import com.aiyan.edu.framework.domain.ucenter.ext.UserToken;
import com.aiyan.edu.framework.domain.ucenter.request.LoginRequest;
import com.aiyan.edu.framework.domain.ucenter.response.AuthCode;
import com.aiyan.edu.framework.domain.ucenter.response.JwtResult;
import com.aiyan.edu.framework.domain.ucenter.response.LoginResult;
import com.aiyan.edu.framework.exception.ExceptionThrow;
import com.aiyan.edu.framework.model.response.CommonCode;
import com.aiyan.edu.framework.model.response.CommonResponseResult;
import com.aiyan.edu.framework.model.response.ResponseResult;
import com.aiyan.edu.framework.utils.CookieUtil;
import com.aiyan.edu.framework.web.BaseController;
import com.aiyan.edu.service.auth.center.service.AuthService;
import com.aiyan.edu.service.auth.center.userdetails.UserJwt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: zqzhou
 * @create: 2019/11/21 20:50
 **/
@RestController
public class AuthController extends BaseController implements AuthControllerApi {

    @Autowired
    AuthService authService;

    @Value("${auth.clientId}")
    String clientId;

    @Value("${auth.clientSecret}")
    String clientSecret;

    @Value("${auth.cookieDomain}")
    String cookieDomain;

    @Value("${auth.cookieMaxAge}")
    Integer cookieMaxAge;

    @Override
    @PostMapping("/login")
    public LoginResult login(LoginRequest loginRequest) {
        if (loginRequest == null || StringUtils.isEmpty(loginRequest.getUsername())) {
            ExceptionThrow.out(AuthCode.AUTH_USERNAME_NONE);
        }

        if (StringUtils.isEmpty(loginRequest.getPassword())) {
            ExceptionThrow.out(AuthCode.AUTH_PASSWORD_NONE);
        }

        AuthToken authToken = this.authService.login(loginRequest.getUsername(),
                loginRequest.getPassword(),
                this.clientId,
                this.clientSecret);

        // 将身份令牌存储到cookie
        String access_token = authToken.getAccess_token();
        CookieUtil.addCookie(super.response, cookieDomain, "/", "utoken", access_token, cookieMaxAge, false);

        return new LoginResult(CommonCode.SUCCESS, access_token);
    }

    @Override
    @PostMapping("/logout")
    public ResponseResult logout() {
        Map<String, String> utokenCookie = CookieUtil.readCookie(super.request, "utoken");
        String utoken = utokenCookie.get("utoken");
        if (StringUtils.isEmpty(utoken)) {
            return ResponseResult.FAIL();
        }

        //清除redis的令牌
        if (!this.authService.logout(utoken)) {
            return ResponseResult.FAIL();
        }

        //清除cookie
        CookieUtil.addCookie(super.response, cookieDomain, "/", "utoken", utoken, 0, false);

        return ResponseResult.SUCCESS();
    }

    @Override
    @GetMapping("/userjwt")
    public JwtResult userJwt() {
        Map<String, String> utokenCookie = CookieUtil.readCookie(super.request, "utoken");
        String utoken = utokenCookie.get("utoken");
        if (StringUtils.isEmpty(utoken)) {
            return new JwtResult(CommonCode.FAIL, null);
        }

        AuthToken userToken = this.authService.userJwt(utoken);
        if (null == userToken) {
            return new JwtResult(CommonCode.FAIL, null);
        }

        return new JwtResult(CommonCode.SUCCESS, userToken.getJwt_token());
    }
}
